// Font Resource
// (c) jimon game studio

#ifndef JEH_JEFONTRESOURCE
#define JEH_JEFONTRESOURCE

#include "IResourceManager.h"
#include "IFontManager.h"

namespace je
{
	namespace media
	{
		//! Font Resource
		class jeFontResource:public je::media::IResource
		{
		protected:
			video::IFont * Font;

		public:
			//! Constructor
			jeFontResource(const jeStringc & ResourceName)
				:IResource(ResourceName),Font(NULL)
			{
			}

			//! Constructor
			jeFontResource(const jeStringc & ResourceName,video::IFont * NewFont)
				:IResource(ResourceName,false),Font(NewFont)
			{
				Font->Grab();
			}

			//! Destructor
			~jeFontResource()
			{
				if(Font)
					Font->Drop();
			}

			//! Get Font
			video::IFont * GetFont()
			{
				return Font;
			}

			//! Load Resource
			void Load()
			{
				core::IDataBase_Query * Query = GetResourceManager()->GetDataBase()->Query(jeStringc("SELECT * FROM FontResources WHERE Name = '")+CurrentResourceName+jeStringc("'"));

				core::IBuffer * Buffer = Query->GetFieldBuffer("Data");
				Font = video::GetFontManager()->GetFont(Buffer);
				Buffer->Drop();

				Query->Drop();
			}

			//! UnLoad Resource
			void UnLoad()
			{
				if(Font)
					Font->Drop();
				Font = NULL;
			}

			//! Get Type Name
			virtual jeStringc GetTypeName()
			{
				return jeStringc("FontResource");
			}
		};

		//! Font Resource Factory
		class jeFontResourceFactory:public je::media::IResourceFactory
		{
		protected:
			typedef jeMap<jeStringc,jeFontResource*> FontMapType;
			FontMapType FontMap;
		public:

			//! Constructor
			jeFontResourceFactory()
			{
			}

			//! Destructor
			~jeFontResourceFactory()
			{
				ClearResources();
			}

			//! Add Resources
			void AddResources(IResource * Resource)
			{
				FontMap.set(Resource->GetName(),static_cast<jeFontResource*>(Resource));
				Resource->Grab();
			}

			//! Load Resources
			void LoadResources()
			{
				ClearResources();

				core::IDataBase_Query * Query = GetResourceManager()->GetDataBase()->Query("SELECT * FROM FontResources");
				while(!Query->Eof())
				{
					jeStringc ResourceName = Query->GetFieldjeStringc("Name");
					jeFontResource * FontResource = NULL;
					JENEW(FontResource,jeFontResource(ResourceName))
					FontMap.set(ResourceName,FontResource);
					Query->NextRow();
				}
				Query->Drop();
			}

			//! Clear Resources
			void ClearResources()
			{
				for(FontMapType::Iterator Iter = FontMap.getIterator();!Iter.atEnd();Iter++)
					Iter->getValue()->Drop();
				FontMap.clear();
			}

			//! Return Resource
			IResource * GetResource(const jeStringc & ResourceName)
			{
				FontMapType::Node * Node = FontMap.find(ResourceName);
				if(Node)
					return Node->getValue();
				else
					return NULL;
			}

			//! Return Resource Type
			jeStringc GetResourceType()
			{
				return jeStringc("FontResource");
			}

			//! Garbage Collector
			void GarbageCollector()
			{
				for(FontMapType::Iterator Iter = FontMap.getIterator();!Iter.atEnd();Iter++)
					if(Iter->getValue()->IsNeedToUnload())
						Iter->getValue()->GarbageUnload();
			}
		};
	}
}

#define JE_FONTRESOURCE( __Name ) JE_RESOURCEBASE_GET(__Name,FontResource)


#endif

